//
//  InviteListTableView.m
//  Cat
//
//  Created by 卢仕彤 on 2019/3/15.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "InviteListTableView.h"
#import "InviteCodeViewController.h"
#import "HttpUserManager.h"
#import "InviteInfoCell.h"
#import "InvitePeopleView.h"
#import "QKShareLiveSheetManager.h"
#import "SearchView.h"
#import "InviteModel.h"

@interface InviteListTableView() <InviteInfoCellDelegate, InvitePeopleViewDelegate>

@property (nonatomic, strong) NSMutableArray *inviteArray;
@property (nonatomic, strong) NSString *phoneString;
@property (nonatomic, strong) NSString *verCodeString;

@end

@implementation InviteListTableView

- (void)setPhoneText:(NSString *)phoneString verCode:(NSString *)verCodeString {
    self.phoneString = phoneString;
    self.verCodeString = verCodeString;
}

- (instancetype)initWithFrame:(CGRect)frame sections:(NSInteger)sections {
    if (self = [super initWithFrame:frame sections:sections]) {
        self.addRefresh = YES;
        self.needLoadMore = YES;
        self.backgroundColor = [UIColor whiteColor];
        self.tableView.backgroundColor = [UIColor whiteColor];
        self.tableView.showsVerticalScrollIndicator = NO;
        self.noFirstAutoRequest = NO;
        self.outerControlLoading = NO;
    }
    return self;
}

- (void)sendRequest {
    if (self.pageIndex == 1) {
        [self.inviteArray removeAllObjects];
    }
    [HttpUserManager getInviteListWithIndex:@(self.pageIndex) success:^(NSArray * _Nonnull inviteList) {
        [self.inviteArray addObjectsFromArray:inviteList];
        [self p_setupTopCellWithArray:self.inviteArray];
        [self.tableView reloadData];
        [self finishLoadData:inviteList];
    } failure:^(NSInteger code, NSString *message) {
        [FLProgressHUD showError:message];
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *sectionCell = [super tableView:tableView cellForRowAtIndexPath:indexPath];
    if (indexPath.section == 0) {
        InviteInfoCell *cell = (InviteInfoCell *)sectionCell;
        cell.delegate = self;
    }
    return sectionCell;
}


- (void)clickinVitePeopleInfo:(InviteModel *)inviteInfo {
    InvitePeopleView *view = [[InvitePeopleView alloc] initWithFrame:CGRectMake(0.0, 0.0, ScreenWidth - kQKFitModuleDecimal(120.0), kQKFitModuleDecimal(189.0))];
    view.delegate = self;
    [view setModel:inviteInfo];
    [QKShareLiveSheetManager showInviteSuperView:ScreenKeyWindow customView:view];
}

- (void)clickInvitePeopleViewWith:(InviteModel *)inviteInfo {
    [HttpUserManager loginWithPhoneNumber:self.phoneString andVerifyCode:self.verCodeString inviteCode:inviteInfo.inviteCode withSuccuss:^(NSString * _Nonnull token) {
        UDToken = token;
        [self.currentNavigationController popToRootViewControllerAnimated:YES];
    } failure:^(NSInteger code, NSString * _Nonnull message) {
        [FLProgressHUD showError:message];
    }];
}

- (void)p_setupTopCellWithArray:(NSArray *)infoArray {
    QKBaseTableCellModel *meTopCellData = [[QKBaseTableCellModel alloc] init];
    meTopCellData.isDynamic = YES;
    meTopCellData.cellClass = @"InviteInfoCell";
    meTopCellData.section = 0;
    meTopCellData.cellData = infoArray;
    meTopCellData.cellHeight = kQKFitModule(60);
    [self addTableSectionData:meTopCellData];
}


#pragma mark lazy init

- (NSMutableArray *)inviteArray {
    if (!_inviteArray) {
        _inviteArray = [[NSMutableArray alloc] init];
    }
    return _inviteArray;
}
@end
